package com.smasher.pictures;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

import androidx.activity.result.ActivityResultLauncher;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.luck.picture.lib.R;
import com.luck.picture.lib.animators.AnimationType;
import com.luck.picture.lib.basic.PictureSelectionCameraModel;
import com.luck.picture.lib.basic.PictureSelectionModel;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.language.LanguageConfig;
import com.luck.picture.lib.style.BottomNavBarStyle;
import com.luck.picture.lib.style.PictureSelectorStyle;
import com.luck.picture.lib.style.SelectMainStyle;
import com.luck.picture.lib.style.TitleBarStyle;
import com.smasher.pictures.engines.GlideEngine;
import com.smasher.pictures.engines.ImageFileCompressEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author moyu
 */
public class PictureHelper {

    private final PictureSelectorStyle selectorStyle;
    private final Params params;

    private PictureHelper(Params params, PictureSelectorStyle style) {
        this.params = params;
        this.selectorStyle = style;
    }


    public void initPicturePreview(Activity context, List<LocalMedia> mSelectList, int position) {
        PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .isPreviewFullScreenMode(true)
                .startActivityPreview(position, false, new ArrayList<>(mSelectList));
    }


    public void initPicturePreview(Fragment context, List<LocalMedia> mSelectList, int position) {
        PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .startFragmentPreview(position, false, new ArrayList<>(mSelectList));
    }


    public void initPicturePreview(Context context, List<LocalMedia> mSelectList, int position) {
        PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .startFragmentPreview(position, false, new ArrayList<>(mSelectList));
    }


    public void initPictureSelect(Activity context,
                                  List<LocalMedia> mSelectList,
                                  ActivityResultLauncher<Intent> launcher) {
        initPictureSelectBase(initPictureSelectActivity(context), mSelectList, launcher);
    }

    public void initPictureSelect(Fragment context,
                                  List<LocalMedia> mSelectList,
                                  ActivityResultLauncher<Intent> launcher) {
        initPictureSelectBase(initPictureSelectFragment(context), mSelectList, launcher);
    }

    public void initPictureSelect(Context context,
                                  List<LocalMedia> mSelectList,
                                  ActivityResultLauncher<Intent> launcher) {
        initPictureSelectBase(initPictureSelect(context), mSelectList, launcher);
    }

    public void initPictureSelect(Activity context,
                                  List<LocalMedia> mSelectList,
                                  OnResultCallbackListener<LocalMedia> call) {
        initPictureSelectBase(initPictureSelectActivity(context), mSelectList, call);
    }

    public void initPictureSelect(Fragment context,
                                  List<LocalMedia> mSelectList,
                                  OnResultCallbackListener<LocalMedia> call) {
        initPictureSelectBase(initPictureSelectFragment(context), mSelectList, call);
    }

    public void initPictureSelect(Context context,
                                  List<LocalMedia> mSelectList,
                                  OnResultCallbackListener<LocalMedia> call) {
        initPictureSelectBase(initPictureSelect(context), mSelectList, call);
    }

    private PictureSelectionModel initPictureSelectFragment(Fragment context) {
        return PictureSelector.create(context)
                //全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .openGallery(params.chooseMode);
    }

    private PictureSelectionModel initPictureSelectActivity(Activity context) {
        return PictureSelector.create(context)
                //全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .openGallery(params.chooseMode);
    }

    private PictureSelectionModel initPictureSelect(Context context) {
        return PictureSelector.create(context)
                //全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .openGallery(params.chooseMode);
    }

    private void initPictureSelectBase(
            PictureSelectionModel model,
            List<LocalMedia> mSelectList,
            ActivityResultLauncher<Intent> launcher) {
        createGalleryModel(model, mSelectList).forResult(launcher);
    }

    private void initPictureSelectBase(
            PictureSelectionModel model,
            List<LocalMedia> mSelectList,
            OnResultCallbackListener<LocalMedia> call) {
        createGalleryModel(model, mSelectList).forResult(call);
    }

    private PictureSelectionModel createGalleryModel(PictureSelectionModel model,
                                                     List<LocalMedia> mSelectList) {
        return model
                .setSelectorUIStyle(selectorStyle)
                .setImageEngine(GlideEngine.createGlideEngine())
                // 外部传入图片加载引擎，必传项
                .setCompressEngine(new ImageFileCompressEngine())
                //SelectModeConfig.SINGLE
                .setSelectionMode(params.selectionMode)
                .setLanguage(params.language)
                .isDisplayTimeAxis(false)
                .isOnlyObtainSandboxDir(false)
                .isPageStrategy(true)
                .isOriginalControl(true)
                .isDisplayCamera(true)
                .isOpenClickSound(true)
                .isFastSlidingSelect(true)
                .isWithSelectVideoImage(false)
                .isPreviewFullScreenMode(true)
                .isPreviewZoomEffect(true)
                .isPreviewImage(true)
                .isPreviewVideo(false)
                .isPreviewAudio(false)
                .isMaxSelectEnabledMask(true)
                .isDirectReturnSingle(false)
                .setMaxSelectNum(params.maxSelectCount)
                .setMinSelectNum(params.minSelectCount)
                .setRecyclerAnimationMode(AnimationType.DEFAULT_ANIMATION)
                .isGif(false)
                .setSelectedData(mSelectList);
    }


    public void takePhoto(Activity context,
                          List<LocalMedia> mSelectList,
                          ActivityResultLauncher<Intent> launcherResult) {
        initTakePhotoBase(initTakePhotoActivity(context), mSelectList, launcherResult);
    }


    public void takePhoto(Fragment context,
                          List<LocalMedia> mSelectList,
                          ActivityResultLauncher<Intent> launcherResult) {
        initTakePhotoBase(initTakePhotoFragment(context), mSelectList, launcherResult);
    }

    public void takePhoto(Context context,
                          List<LocalMedia> mSelectList,
                          ActivityResultLauncher<Intent> launcherResult) {
        initTakePhotoBase(initTakePhoto(context), mSelectList, launcherResult);
    }


    public void takePhoto(Activity context,
                          List<LocalMedia> mSelectList,
                          OnResultCallbackListener<LocalMedia> call) {
        initTakePhotoBase(initTakePhotoActivity(context), mSelectList, call);
    }


    public void takePhoto(Fragment context,
                          List<LocalMedia> mSelectList,
                          OnResultCallbackListener<LocalMedia> call) {
        initTakePhotoBase(initTakePhotoFragment(context), mSelectList, call);
    }

    public void takePhoto(Context context,
                          List<LocalMedia> mSelectList,
                          OnResultCallbackListener<LocalMedia> call) {
        initTakePhotoBase(initTakePhoto(context), mSelectList, call);
    }


    private PictureSelectionCameraModel initTakePhotoActivity(Activity context) {
        return PictureSelector.create(context).openCamera(params.chooseMode);
    }


    private PictureSelectionCameraModel initTakePhotoFragment(Fragment context) {
        return PictureSelector.create(context).openCamera(params.chooseMode);
    }

    private PictureSelectionCameraModel initTakePhoto(Context context) {
        return PictureSelector.create(context).openCamera(params.chooseMode);
    }

    private void initTakePhotoBase(
            PictureSelectionCameraModel model,
            List<LocalMedia> mSelectList,
            ActivityResultLauncher<Intent> launcher) {

        createCameraModel(model, mSelectList)
                .forResultActivity(launcher);
    }

    private void initTakePhotoBase(
            PictureSelectionCameraModel model,
            List<LocalMedia> mSelectList,
            OnResultCallbackListener<LocalMedia> call) {

        createCameraModel(model, mSelectList)
                .forResultActivity(call);
    }


    private PictureSelectionCameraModel createCameraModel(PictureSelectionCameraModel model,
                                                          List<LocalMedia> mSelectList) {

        return model.setCompressEngine(new ImageFileCompressEngine())
                .setLanguage(params.language)
                .setSelectedData(mSelectList);
    }


    public static class Builder {
        Params params = new Params();

        private final PictureSelectorStyle selectorStyle = new PictureSelectorStyle();

        public Builder setMaxSelectCount(int maxSelectCount) {
            params.maxSelectCount = maxSelectCount;
            return this;
        }

        public Builder setMinSelectCount(int minSelectCount) {
            params.minSelectCount = minSelectCount;
            return this;
        }

        /**
         * span
         *
         * @param imageSpanCount imageSpanCount
         * @return Builder
         */
        public Builder setImageSpanCount(int imageSpanCount) {
            params.imageSpanCount = imageSpanCount;
            return this;
        }

        /**
         * 选择类型
         *
         * @param chooseMode 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
         * @return Builder
         */
        public Builder setChooseMode(int chooseMode) {
            params.chooseMode = chooseMode;
            return this;
        }

        /**
         * 语言
         *
         * @param language language 语言
         * @return Builder
         */
        public Builder setLanguage(int language) {
            params.language = language;
            return this;
        }

        /**
         * 选择模式
         *
         * @param selectionMode 多选 {@link SelectModeConfig#MULTIPLE} 单选{@link SelectModeConfig#SINGLE}
         * @return Builder
         */
        public Builder setSelectionMode(int selectionMode) {
            params.selectionMode = selectionMode;
            return this;
        }

        public Builder setMinimumCompressSize(int minimumCompressSize) {
            params.minimumCompressSize = minimumCompressSize;
            return this;
        }

        public PictureHelper build(Context context) {
            buildStyle(context, 1, selectorStyle);
            return new PictureHelper(params, selectorStyle);
        }

        public void buildStyle(Context context, int type, PictureSelectorStyle selectorStyle) {
            if (type == 0) {
                defineWhiteStyle(context, selectorStyle);
            } else if (type == 1) {
                defineWhiteStyle(context, selectorStyle);
            } else if (type == 2) {
                defineWhiteStyle(context, selectorStyle);
            }
        }

        private void defineWhiteStyle(Context context, PictureSelectorStyle selectorStyle) {
            TitleBarStyle whiteTitleBarStyle = new TitleBarStyle();
            whiteTitleBarStyle.setTitleBackgroundColor(ContextCompat.getColor(context, R.color.ps_color_white));
            whiteTitleBarStyle.setTitleDrawableRightResource(R.drawable.ps_ic_default_arrow);
            whiteTitleBarStyle.setTitleLeftBackResource(R.drawable.ps_ic_black_back);
            whiteTitleBarStyle.setTitleTextColor(ContextCompat.getColor(context, R.color.ps_color_black));
            whiteTitleBarStyle.setTitleCancelTextColor(ContextCompat.getColor(context, R.color.ps_color_53575e));
            whiteTitleBarStyle.setDisplayTitleBarLine(true);

            BottomNavBarStyle whiteBottomNavBarStyle = new BottomNavBarStyle();
            whiteBottomNavBarStyle.setBottomNarBarBackgroundColor(Color.parseColor("#EEEEEE"));
            whiteBottomNavBarStyle.setBottomPreviewSelectTextColor(ContextCompat.getColor(context, R.color.ps_color_53575e));

            whiteBottomNavBarStyle.setBottomPreviewNormalTextColor(ContextCompat.getColor(context, R.color.ps_color_9b));
            whiteBottomNavBarStyle.setBottomPreviewSelectTextColor(ContextCompat.getColor(context, R.color.ps_color_fa632d));
            whiteBottomNavBarStyle.setCompleteCountTips(false);
            whiteBottomNavBarStyle.setBottomEditorTextColor(ContextCompat.getColor(context, R.color.ps_color_53575e));
            whiteBottomNavBarStyle.setBottomOriginalTextColor(ContextCompat.getColor(context, R.color.ps_color_53575e));

            SelectMainStyle selectMainStyle = new SelectMainStyle();
            selectMainStyle.setStatusBarColor(ContextCompat.getColor(context, R.color.ps_color_white));
            selectMainStyle.setDarkStatusBarBlack(true);
            selectMainStyle.setSelectNormalTextColor(ContextCompat.getColor(context, R.color.ps_color_9b));
            selectMainStyle.setSelectTextColor(ContextCompat.getColor(context, R.color.ps_color_fa632d));
            selectMainStyle.setPreviewSelectBackground(R.drawable.ps_preview_checkbox_selector);
            selectMainStyle.setSelectBackground(R.drawable.ps_checkbox_selector);
            selectMainStyle.setSelectText(context.getString(R.string.ps_done_front_num));
            selectMainStyle.setMainListBackgroundColor(ContextCompat.getColor(context, R.color.ps_color_white));

            selectorStyle.setTitleBarStyle(whiteTitleBarStyle);
            selectorStyle.setBottomBarStyle(whiteBottomNavBarStyle);
            selectorStyle.setSelectMainStyle(selectMainStyle);
        }

    }


    private static class Params {
        //最大图片选择数量 int
        protected int maxSelectCount;
        protected int minSelectCount;
        protected int imageSpanCount;

        protected int selectionMode;
        protected int minimumCompressSize;

        protected int chooseMode;
        protected int language;


        public Params() {
            maxSelectCount = 3;
            minSelectCount = 1;
            imageSpanCount = 3;
            minimumCompressSize = 100;
            chooseMode = SelectMimeType.ofImage();
            language = LanguageConfig.CHINESE;
            selectionMode = SelectModeConfig.MULTIPLE;
        }
    }

}
